package hos.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.appcompat.widget.LinearLayoutCompat;

import org.sufficientlysecure.htmltextview.HtmlTextView;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import hos.adapter.CommonArrayAdapter;
import hos.ui.bean.CheckMore;
import hos.ui.bean.CheckSingle;
import hos.ui.radio.RadioGroupExtend;
import hos.ui.spinner.SpinnerExtend;
import hos.ui.time.DateText;
import hos.ui.R;

/**
 * <p>Title: InputItemLayout </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2022/4/5 20:44
 */
public class InputItemLayout extends LinearLayoutCompat {
    private TextView titleView;
    private EditText editText;
    private TextView textView;
    private RadioGroupExtend group;
    private ImageView imageView;
    private SpinnerExtend spinnerPro;
    private DateText dateText;
    private Line bottomLine;
    private Line topLine;
    private final Paint topPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Paint bottomPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int maxInputLength = 20;
    private boolean isMustFull = false;

    private TextView mustView;
    private LinearLayout titleLayout;
    private int titleWidth;
    private String key;
    private int spinnerColor;
    private int spinnerSize;
    private int spinnerDownColor;
    private int spinnerDownSize;

    public InputItemLayout(Context context) {
        this(context, null);
    }

    public InputItemLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InputItemLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setShowDividers(SHOW_DIVIDER_BEGINNING);
        setDividerDrawable(new ColorDrawable());
        if (context == null) {
            return;
        }
        TypedArray typedArray =
                context.obtainStyledAttributes(attrs, R.styleable.InputItemLayout);

        try {
            int titleStyleId =
                    typedArray.getResourceId(R.styleable.InputItemLayout_titleTextAppearance, 0);

            String title = typedArray.getString(R.styleable.InputItemLayout_android_title);
            isMustFull = typedArray.getBoolean(R.styleable.InputItemLayout_must, false);

            maxInputLength = typedArray.getInteger(R.styleable.InputItemLayout_maxInputLength, Integer.MAX_VALUE);

            //解析title资源属性
            parseTitleStyle(titleStyleId, title);

            if (isMustFull) {
                setMustFull();
            }
            if (titleLayout != null) {
                addView(titleLayout);
            }
            //解析右侧的输入框属性
            String hint = typedArray.getString(R.styleable.InputItemLayout_android_hint);
            int inputResId =
                    typedArray.getResourceId(R.styleable.InputItemLayout_inputTextAppearance, 0);
            int inputType = typedArray.getInteger(R.styleable.InputItemLayout_itemInputType, 0);
            if (inputType == 3) {
                final CharSequence[] entries = typedArray.getTextArray(R.styleable.InputItemLayout_android_entries);
                parseDateStyle(hint, inputResId, entries);
            } else if (inputType == 7) {
                parseTextStyle(generateTextView(), hint, inputResId);
            } else if (inputType == 9) {
                parseTextStyle(generateHtmlTextView(), hint, inputResId);
            } else if (inputType == 10) {
                final CharSequence[] entries = typedArray.getTextArray(R.styleable.InputItemLayout_android_entries);
                parseRadioGroup(generateRadioGroup(), inputResId, entries);
            } else if (inputType == 8) {
                parseImageStyle();
            } else if (inputType == 4) {
                parseDateStyle(hint, inputResId);
            } else {
                parseInputStyle(hint, inputResId, inputType);
            }
            //上下分割线属性
            int topResId = typedArray.getResourceId(R.styleable.InputItemLayout_topLineAppearance, 0);
            int bottomResId =
                    typedArray.getResourceId(R.styleable.InputItemLayout_bottomLineAppearance, 0);
            topLine = parseLineStyle(topResId);
            bottomLine = parseLineStyle(bottomResId);
            if (topLine.enable) {
                topPaint.setColor(topLine.color);
                topPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                topPaint.setStrokeWidth(topLine.height);
            }

            if (bottomLine.enable) {
                bottomPaint.setColor(bottomLine.color);
                bottomPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                bottomPaint.setStrokeWidth(bottomLine.height);
            }
        } finally {
            typedArray.recycle();
        }
    }


    private Line parseLineStyle(int resId) {
        Line line = new Line();
        TypedArray array =
                getContext().obtainStyledAttributes(resId, R.styleable.lineAppearance);
        line.color = array.getColor(
                R.styleable.lineAppearance_color,
                Color.parseColor("#D1D2D3")
        );
        line.height = array.getDimensionPixelOffset(R.styleable.lineAppearance_height, 0);
        line.leftMargin =
                array.getDimensionPixelOffset(R.styleable.lineAppearance_leftMargin, 0);
        line.rightMargin =
                array.getDimensionPixelOffset(R.styleable.lineAppearance_rightMargin, 0);
        line.enable = array.getBoolean(R.styleable.lineAppearance_enable, false);
        array.recycle();
        return line;

    }

    private void parseTextStyle(TextView target, String hint, int resId) {
        TypedArray array = getContext().obtainStyledAttributes(resId, R.styleable.inputTextAppearance);
        int hintColor = array.getColor(
                R.styleable.inputTextAppearance_hintColor,
                Color.parseColor("#D1D2D3")
        );
        int inputColor = array.getColor(
                R.styleable.inputTextAppearance_inputColor,
                Color.parseColor("#565A5A")
        );
        //px
        int textSize = array.getDimensionPixelSize(
                R.styleable.inputTextAppearance_textSize,
                applyUnit(TypedValue.COMPLEX_UNIT_SP, 15f)
        );
        int textLines = array.getInt(
                R.styleable.inputTextAppearance_lines,
                1
        );
        Drawable background = array.getDrawable(
                R.styleable.inputTextAppearance_background
        );


        textView = target;

        InputFilter lengthFilter = new InputFilter.LengthFilter(maxInputLength);
        textView.setFilters(new InputFilter[]{lengthFilter});//最多可输入的字符数
        if (background != null) {
            textView.setBackground(background);
        } else {
            textView.setBackgroundColor(Color.TRANSPARENT);
        }
        int orientation = getOrientation();
        if (orientation == HORIZONTAL) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            textView.setLayoutParams(params);
            textView.setGravity(Gravity.LEFT | Gravity.CENTER);
            textView.setPadding(0, 0, 0, 0);
            textView.setBackgroundResource(R.drawable.bg_flat_radius16);
        } else {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.bottomMargin = 40;
            textView.setLayoutParams(params);
            textView.setGravity(Gravity.LEFT | Gravity.TOP);
            textView.setPadding(0, 0, 0, 0);
            textView.setBackgroundResource(R.drawable.bg_flat_radius16);
        }
        textView.setMinLines(textLines);
        textView.setHint(hint);
        textView.setTextColor(inputColor);
        textView.setHintTextColor(hintColor);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);

        addView(textView);
        array.recycle();
    }

    private void parseRadioGroup(RadioGroupExtend generateRadioGroup, int inputResId, CharSequence[] entries) {
        group = generateRadioGroup;
        int orientation = getOrientation();
        group.setOrientation(RadioGroupExtend.HORIZONTAL);
        if (orientation == HORIZONTAL) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
            params.weight = 1f;
            params.gravity = Gravity.RIGHT;
            group.setLayoutParams(params);
            group.setPadding(0, 0, 0, 0);
        } else {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.bottomMargin = 40;
            params.gravity = Gravity.RIGHT;
            group.setLayoutParams(params);
            group.setPadding(0, 0, 0, 0);
        }
        addView(group);
        group.add(entries);
    }

    private void parseImageStyle() {
        imageView = generateImageView();
        int orientation = getOrientation();
        if (orientation == HORIZONTAL) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
            params.weight = 1f;
            imageView.setLayoutParams(params);
            imageView.setPadding(0, 0, 0, 0);
        } else {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.bottomMargin = 40;
            imageView.setLayoutParams(params);
            imageView.setPadding(0, 0, 0, 0);
            imageView.setBackgroundResource(R.drawable.bg_flat_radius16);
        }
        addView(imageView);
    }

    private void parseInputStyle(String hint, int resId, int inputType) {
        TypedArray array = getContext().obtainStyledAttributes(resId, R.styleable.inputTextAppearance);
        int hintColor = array.getColor(
                R.styleable.inputTextAppearance_hintColor,
                Color.parseColor("#D1D2D3")
        );
        int inputColor = array.getColor(
                R.styleable.inputTextAppearance_inputColor,
                Color.parseColor("#565A5A")
        );
        //px
        int textSize = array.getDimensionPixelSize(
                R.styleable.inputTextAppearance_textSize,
                applyUnit(TypedValue.COMPLEX_UNIT_SP, 15f)
        );
        int textLines = array.getInt(
                R.styleable.inputTextAppearance_lines,
                1
        );
        Drawable background = array.getDrawable(
                R.styleable.inputTextAppearance_background
        );


        editText = generateEditText();

        /**
         * <enum name="text" value="0"></enum>
         * <enum name="password" value="1"></enum>
         * <enum name="number" value="2"></enum>
         */
        if (inputType == 0) {
            editText.setInputType(InputType.TYPE_CLASS_TEXT);
        } else if (inputType == 1) {
            editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | (InputType.TYPE_CLASS_TEXT));
        } else if (inputType == 2) {
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        } else if (inputType == 5) {
            editText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        }

        InputFilter lengthFilter = new InputFilter.LengthFilter(maxInputLength);
        editText.setFilters(new InputFilter[]{lengthFilter});//最多可输入的字符数
        if (background != null) {
            editText.setBackground(background);
        } else {
            editText.setBackgroundColor(Color.TRANSPARENT);
        }
        int orientation = getOrientation();
        if (orientation == HORIZONTAL) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
            params.weight = 1f;
            editText.setLayoutParams(params);
            editText.setGravity(Gravity.LEFT | Gravity.CENTER);
            editText.setPadding(0, 0, 0, 0);
        } else {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.bottomMargin = 40;
            editText.setLayoutParams(params);
            editText.setGravity(Gravity.LEFT | Gravity.TOP);
            editText.setPadding(0, 0, 0, 0);
            editText.setBackgroundResource(R.drawable.bg_flat_radius16);
        }
        editText.setMinLines(textLines);
        editText.setHint(hint);
        editText.setTextColor(inputColor);
        editText.setHintTextColor(hintColor);
        editText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);

        addView(editText);
        array.recycle();
    }

    public void setRightBackgroundResource(int id) {
        int orientation = getOrientation();
        if (orientation == VERTICAL) {
            if (editText != null) {
                editText.setBackgroundResource(id);
            }
            if (imageView != null) {
                imageView.setBackgroundResource(id);
            }
            if (textView != null) {
                textView.setBackgroundResource(id);
            }
        }
    }

    private void parseDateStyle(String hint, int resId, CharSequence[] entries) {
        TypedArray array = getContext().obtainStyledAttributes(resId, R.styleable.spinnerAppearance);
        spinnerColor = array.getColor(
                R.styleable.spinnerAppearance_spinnerColor,
                Color.parseColor("#565A5A")
        );
        //px
        spinnerSize = array.getDimensionPixelSize(
                R.styleable.spinnerAppearance_spinnerSize,
                applyUnit(TypedValue.COMPLEX_UNIT_SP, 15f)
        );
        spinnerDownColor = array.getColor(
                R.styleable.spinnerAppearance_spinnerDownColor,
                Color.parseColor("#565A5A")
        );
        //px
        spinnerDownSize = array.getDimensionPixelSize(
                R.styleable.spinnerAppearance_spinnerDownSize,
                applyUnit(TypedValue.COMPLEX_UNIT_SP, 15f)
        );
        Drawable background = array.getDrawable(
                R.styleable.spinnerAppearance_spinnerBackground
        );

        spinnerPro = generateSpinner();

        int orientation = getOrientation();
        if (orientation == HORIZONTAL) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//            params.weight = 1f;
            spinnerPro.setLayoutParams(params);
            spinnerPro.setGravity(Gravity.LEFT | Gravity.CENTER);
            spinnerPro.setPadding(0, 0, 0, 0);
        } else {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            spinnerPro.setLayoutParams(params);
            spinnerPro.setGravity(Gravity.LEFT | Gravity.TOP);
            spinnerPro.setPadding(0, 20, 0, 40);
        }
        if (background != null) {
            spinnerPro.setBackground(background);
        }
        if (entries == null) {
            entries = new CharSequence[]{};
        }
        bindSpinner(Arrays.asList(entries));

        addView(spinnerPro);
        array.recycle();
    }

    public <T> void bindSpinner(List<T> entries) {
        final CommonArrayAdapter<T> adapter = new CommonArrayAdapter<T>(
                getContext(), android.R.layout.simple_spinner_item, entries) {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View target = super.getView(position, convertView, parent);
                if (target instanceof TextView) {
                    if (spinnerColor != -1) {
                        ((TextView) target).setTextColor(spinnerColor);
                    }
                    if (spinnerSize != -1) {
                        ((TextView) target).setTextSize(TypedValue.COMPLEX_UNIT_PX, spinnerSize);
                    }
                }
                return target;
            }

            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                View target = super.getDropDownView(position, convertView, parent);
                if (target instanceof TextView) {
                    if (spinnerColor != -1) {
                        ((TextView) target).setTextColor(spinnerDownColor);
                    }
                    if (spinnerSize != -1) {
                        ((TextView) target).setTextSize(TypedValue.COMPLEX_UNIT_PX, spinnerDownSize);
                    }
                }
                return target;
            }
        };
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerPro.setAdapter(adapter);
    }


    private void parseDateStyle(String hint, int resId) {
        TypedArray array = getContext().obtainStyledAttributes(resId, R.styleable.dateAppearance);
        int textColor = array.getColor(
                R.styleable.dateAppearance_dateColor,
                Color.parseColor("#565A5A")
        );
        //px
        int textSize = array.getDimensionPixelSize(
                R.styleable.dateAppearance_dateSize,
                applyUnit(TypedValue.COMPLEX_UNIT_SP, 15f)
        );
        boolean thisDay = array.getBoolean(
                R.styleable.dateAppearance_thisDay,
                false
        );
        boolean isFull = array.getBoolean(
                R.styleable.dateAppearance_isFull,
                true
        );
        String format = array.getString(R.styleable.dateAppearance_dateFormat);
        if (TextUtils.isEmpty(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        Drawable background = array.getDrawable(
                R.styleable.dateAppearance_dateBackground
        );

        dateText = generateDateText();
        dateText.setTextColor(textColor);
        dateText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        dateText.setFull(isFull);
        dateText.setFormat(format);
        dateText.setHint(hint);

        int orientation = getOrientation();
        if (orientation == HORIZONTAL) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            params.weight = 1f;
            dateText.setLayoutParams(params);
            dateText.setGravity(Gravity.LEFT | Gravity.CENTER);
            dateText.setPadding(0, 0, 0, 0);
        } else {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            dateText.setLayoutParams(params);
            dateText.setGravity(Gravity.LEFT | Gravity.TOP);
            dateText.setPadding(0, 20, 0, 40);
        }
        if (background != null) {
            dateText.setBackground(background);
        } else {
            dateText.setBackgroundColor(Color.TRANSPARENT);
        }

        addView(dateText);
        array.recycle();
    }


    protected ImageView generateImageView() {
        return new ImageView(getContext());
    }


    protected TextView generateHtmlTextView() {
        return new HtmlTextView(getContext());
    }


    protected RadioGroupExtend generateRadioGroup() {
        return new RadioGroupExtend(getContext());
    }


    protected RadioButton generateRadioButton() {
        return new RadioButton(getContext());
    }


    protected TextView generateTextView() {
        return new TextView(getContext());
    }


    protected EditText generateEditText() {
        return new EditText(getContext());
    }


    protected SpinnerExtend generateSpinner() {
        return new SpinnerExtend(getContext());
    }


    protected DateText generateDateText() {
        return new DateText(getContext());
    }

    private void parseTitleStyle(int resId, String title) {
        TypedArray array = getContext().obtainStyledAttributes(resId, R.styleable.titleTextAppearance);
        int titleColor = array.getColor(
                R.styleable.titleTextAppearance_titleColor,
                Color.parseColor("#565A5A")
        );

        //px
        int titleSize = array.getDimensionPixelSize(
                R.styleable.titleTextAppearance_titleSize,
                applyUnit(TypedValue.COMPLEX_UNIT_SP, 15f)
        );

        int minWidth = array.getDimensionPixelOffset(R.styleable.titleTextAppearance_titleMinWidth, 0);
        titleWidth = array.getDimensionPixelOffset(R.styleable.titleTextAppearance_titleWidth, 0);

        titleView = new TextView(getContext());
        titleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);  //sp---当做sp在转换一次
        titleView.setTextColor(titleColor);
        int orientation = getOrientation();
        if (orientation == HORIZONTAL) {
            if (titleWidth == 0) {
                titleWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
            }
            titleView.setLayoutParams(new LinearLayout.LayoutParams(titleWidth, LinearLayout.LayoutParams.MATCH_PARENT));
        } else {
            if (titleWidth == 0) {
                titleWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
            }
            titleView.setLayoutParams(new LinearLayout.LayoutParams(titleWidth, LinearLayout.LayoutParams.WRAP_CONTENT));
            titleView.setPadding(0, 40, 0, 40);
            titleLayout = new LinearLayout(getContext());
            titleLayout.setOrientation(LinearLayout.HORIZONTAL);
        }
        titleView.setGravity(Gravity.LEFT | Gravity.CENTER);
        titleView.setMinWidth(minWidth);
        setTitle(title);
        if (titleLayout == null) {
            addView(titleView);
        } else {
            titleLayout.addView(titleView);
        }
        array.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //巨坑
        if (topLine.enable) {
            canvas.drawLine(
                    topLine.leftMargin,
                    0f,
                    getMeasuredWidth() - topLine.rightMargin,
                    0f,
                    topPaint
            );
        }

        if (bottomLine.enable) {
            canvas.drawLine(
                    bottomLine.leftMargin,
                    getHeight() - bottomLine.height,
                    getMeasuredWidth() - bottomLine.rightMargin,
                    getHeight() - bottomLine.height,
                    bottomPaint
            );
        }
    }

    public static class Line {

        int color = 0;
        float height = 0f;
        float leftMargin = 0f;
        float rightMargin = 0f;
        boolean enable = false;
    }

    private int applyUnit(int applyUnit, float value) {
        return (int) TypedValue.applyDimension(applyUnit, value, getResources().getDisplayMetrics());
    }

    public TextView getTitleView() {
        return titleView;
    }

    public void setTitle(String title) {
        titleView.setText(title);
    }

    public String getTitle() {
        if (titleView.getText() == null) {
            return "";
        }
        String title = String.valueOf(titleView.getText());
        return title.replaceAll(":", "").replaceAll("：", "");
    }

    public void setContent(Map<String, Object> map, String key) {
        setKey(key);
        if (editText == null) {
            return;
        }
        if (!map.containsKey(key)) {
            return;
        }
        Object value = map.get(key);
        if (value == null) {
            editText.setText("");
            return;
        }
        editText.setText(String.valueOf(value));
    }

    public void setContent(String content) {
        if (editText != null) {
            editText.setText(content);
        }
        if (dateText != null) {
            dateText.setText(content);
        }
        if (textView != null) {
            textView.setText(content);
        }
    }

    public ImageView getImageView() {
        return imageView;
    }

    public TextView getTextView() {
        return textView;
    }

    public HtmlTextView getHtmlTextView() {
        return (HtmlTextView) textView;
    }

    public EditText getEditText() {
        return editText;
    }

    public SpinnerExtend getSpinner() {
        return spinnerPro;
    }

    public DateText getDateText() {
        return dateText;
    }

    public TextView getMustView() {
        return mustView;
    }

    public void showMust() {
        if (mustView != null) {
            mustView.setVisibility(VISIBLE);
        }
    }

    public void hideMust() {
        if (mustView != null) {
            mustView.setVisibility(GONE);
        }
    }

    public CharSequence getText() {
        EditText editText = getEditText();
        if (editText != null) {
            return editText.getText();
        }
        DateText dateText = getDateText();
        if (dateText != null) {
            return dateText.getText();
        }
        TextView textView = getTextView();
        if (textView != null) {
            return textView.getText();
        }
        if (group != null) {
            if (group.getTag() == null) {
                return null;
            }
            return (CharSequence) group.getTag();
        }
        return (CharSequence) spinnerPro.getSelectedItem();

    }

    public void clear() {
        if (editText != null) {
            editText.setText(null);
        }
        if (group != null) {
            group.clearCheck();
            group.setTag(null);
        }
    }

    public Line bottomLine() {
        return bottomLine;
    }

    public Line topLine() {
        return topLine;
    }


    public String getTrimmedString() {
        CharSequence charSequence = getText();
        if (charSequence != null) {
            return charSequence.toString();
        }
        if (spinnerPro != null) {
            return (String) spinnerPro.getSelectedItem();
        }
        return "";
    }

    private void setMustFull() {
        isMustFull = true;
        if (mustView != null) {
            return;
        }

        int width = dp2px(16);
        float sp16 = sp2px(16);
        mustView = new TextView(getContext());
        mustView.setTextSize(TypedValue.COMPLEX_UNIT_PX, sp16);  //sp---当做sp在转换一次
        mustView.setTextColor(Color.RED);
        mustView.setLayoutParams(new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.WRAP_CONTENT));
        mustView.setText("*");
        mustView.setGravity(Gravity.CENTER);
        if (titleLayout != null) {
            titleLayout.addView(mustView);
        } else {
            addView(mustView);
        }
//        TextView titleView = getTitleView();
//        CharSequence title = titleView.getText() + "*";
//        SpannableStringBuilder style = new SpannableStringBuilder(title);
//        style.setSpan(new ForegroundColorSpan(Color.RED), title.length() - 1, title.length(),
//                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//        titleView.setText(style);
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public void addTextChangedListener(TextWatcher watcher) {
        editText.addTextChangedListener(watcher);
    }

    public void removeTextChangedListener(TextWatcher watcher) {
        editText.removeTextChangedListener(watcher);
    }

    @Override
    public void setClickable(boolean clickable) {
        if (editText != null) {
            editText.setClickable(clickable);
        }
        if (dateText != null) {
            dateText.setClickable(clickable);
        }
        if (textView != null) {
            textView.setClickable(clickable);
        }
        if (spinnerPro != null) {
            spinnerPro.setClickable(clickable);
        }
        super.setClickable(clickable);
    }

    @Override
    public void setEnabled(boolean enabled) {
        if (editText != null) {
            editText.setEnabled(enabled);
        }
        if (dateText != null) {
            dateText.setOnClickListener(null);
            dateText.setEnabled(enabled);
        }
        if (textView != null) {
            textView.setOnClickListener(null);
            textView.setEnabled(enabled);
        }
        if (spinnerPro != null) {
            spinnerPro.setEnabled(enabled);
        }
        super.setEnabled(enabled);
    }

    private void removeRightView() {
        int childCount = getChildCount();
        if (childCount == 2) {
            removeViewAt(1);
        } else if (childCount == 3) {
            removeViewAt(2);
        }
    }

    public void addRadioArray(CharSequence[] textList) {
        group.addArray(textList);
    }

    public void addRadio(CharSequence... textList) {
        if (group == null) {
            return;
        }
        group.add(textList);
    }

    public void setOnCheckedChangeListener(RadioGroupExtend.OnCheckedListener listener) {
        if (group == null) {
            return;
        }
        group.setOnCheckedListener(listener);
    }


    /**
     * Value of dp to value of px.
     *
     * @param dpValue The value of dp.
     * @return value of px
     */
    public static int dp2px(final float dpValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    /**
     * Value of sp to value of px.
     *
     * @param spValue The value of sp.
     * @return value of px
     */
    public static int sp2px(final float spValue) {
        final float fontScale = Resources.getSystem().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }


    /**
     * 多选
     *
     * @param checkMore 选项
     */
    public void showMoreDialog(CheckMore checkMore, String split, View.OnClickListener listener) {
        new AlertDialog.Builder(getContext())
                .setTitle("请选择")
                .setMultiChoiceItems(checkMore.getTitleArray(), checkMore.getBoolArray(), new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        checkMore.check(which, isChecked);
                    }
                })
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String[] check = checkMore.getCheck(split);
                        setContent(check[0]);
                        setTag(check[1]);
                        if (listener != null) {
                            listener.onClick(InputItemLayout.this);
                        }
                    }
                }).setPositiveButton("取消", null)
                .show();
    }

    /**
     * 单选
     *
     * @param checkSingle 选项
     */
    public void showSingleDialog(CheckSingle checkSingle, View.OnClickListener listener) {
        new AlertDialog.Builder(getContext())
                .setTitle("请选择")
                .setSingleChoiceItems(checkSingle.getTitleArray(), checkSingle.getIndex(), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        setContent(checkSingle.title(which));
                        setTag(checkSingle.check(which));
                        dialog.dismiss();
                        if (listener != null) {
                            listener.onClick(InputItemLayout.this);
                        }
                    }
                })
                .show();
    }
}
